//
//  TypeAllTopicViewController.m
//  LiWuShuo
//
//  Created by 袁小航 on 14/11/18.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "TypeAllTopicViewController.h"
#import "TypeAllTopic.h"
#import "Define.h"
#import "TypeAllTopicCell.h"
#import "ShowProViewController.h"
#import "TypeAllTopicModel.h"
#import <UIImageView+AFNetworking.h>

@interface TypeAllTopicViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic, retain) TypeAllTopic *allTopic;
//创建一个用来保存allTopic数据的数组
@property(nonatomic, retain) NSArray *allTopicData;

@end

@implementation TypeAllTopicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UILabel * allTopic = [[UILabel alloc]init];
    allTopic.text = @"全部专题";
    allTopic.textColor = [UIColor whiteColor];
    allTopic.font = [UIFont systemFontOfSize:16];
    allTopic.frame = CGRectMake(0, 0, 40, 30);
    self.navigationItem.titleView = allTopic;
    
//    获取数据
    [self getAllTopicData];
    
//    创建typeAllTopic视图
    [self createTypeAllTopicView];
    
//    创建自定义返回按钮
    [self createBackButton];
}
#pragma mark - 获取数据
- (void)getAllTopicData
{
    NSURLSessionDataTask *allTopicData = [TypeAllTopicModel allTopicWithBlock:^(NSArray *posts, NSError *error) {
        _allTopicData = posts;
        if (_allTopicData.count == 0) {
            UIAlertView *netError = [[UIAlertView alloc]initWithTitle:@"网络连接" message:@"网络异常，请重试..." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [netError show];
        }
        [_allTopic.allTopicCollectionView reloadData];
    }];
}

#pragma mark - 创建自定义返回按钮
- (void)createBackButton
{
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    back.frame = CGRectMake(0, 0, 25, 25);
    [back setBackgroundImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [back addTarget:self action:@selector(backPageAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:back];
    self.navigationItem.leftBarButtonItem = backItem;
}
#pragma mark - 自定义的返回按钮的 响应方法
- (void)backPageAction:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 创建typeAllTopic视图
- (void)createTypeAllTopicView
{
    self.allTopic = [[TypeAllTopic alloc]initWithFrame:CGRectMake(0, 0, MAIN_SCREEN_WIDTH, MAIN_SCREEN_HEIGHT - 60)];
    
    _allTopic.allTopicCollectionView.delegate = self;
    _allTopic.allTopicCollectionView.dataSource = self;
    
    [_allTopic.allTopicCollectionView registerClass:[TypeAllTopicCell class] forCellWithReuseIdentifier:@"allTopicCell"];
    
    [self.view addSubview:_allTopic];
}

#pragma mark - CollectionView delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (_allTopicData != nil) {
        return _allTopicData.count;
    } else {
        return 0;
    }
    return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TypeAllTopicCell *allTopicCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"allTopicCell" forIndexPath:indexPath];
    allTopicCell.layer.cornerRadius = 4;
    allTopicCell.layer.masksToBounds = YES;
    
    NSURL *imageUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@", [_allTopicData[indexPath.row] allTopicImage]]];
    [allTopicCell.allTopicImage setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"placehold.png"]];
    
    UIVisualEffectView *maskingView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]];
    maskingView.frame = CGRectMake(0, 0, allTopicCell.frame.size.width, allTopicCell.frame.size.height);
    maskingView.alpha = 0.1;
    [allTopicCell.allTopicImage addSubview:maskingView];
    
    allTopicCell.allTopicImage.contentMode = UIViewContentModeScaleAspectFill;
    allTopicCell.title.text = [_allTopicData[indexPath.row] allTopicTitle];
    allTopicCell.subTitle.text = [_allTopicData[indexPath.row] allTopicSubTitle];
    
    return allTopicCell;
}

#pragma mark - didSelected
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ShowProViewController *showVc = [[ShowProViewController alloc]init];
    if (_allTopicData.count != 0) {
        showVc.subID = [_allTopicData[indexPath.row] allTopicId];
        showVc.subType = @"Topic";
        showVc.subTitle = [_allTopicData[indexPath.row] allTopicTitle];
    }
    [self.navigationController pushViewController:showVc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
